import { clearAll, QXEvent } from 'qx-util';
import { TerminalMessageItem } from '../../entities';
import { GroupMessageService } from '../group-message/group-message.service';

/**
 * console区消息匹配服务
 *
 * @author chitanda
 * @date 2021-06-23 10:06:31
 * @export
 * @class TerminalMessageBaseService
 */
export class TerminalMessageBaseService {
  readonly evt: QXEvent<{ message: TerminalMessageItem }> = new QXEvent();

  constructor(protected group?: GroupMessageService) {}

  /**
   * 分割线
   *
   * @author chitanda
   * @date 2021-06-23 11:06:16
   * @type {string}
   */
  readonly splitLineText: string =
    '\x1b[33m-----------------------------------------历史记录----------------------------------------- \x1b[0m';

  /**
   * 已接收消息缓存
   *
   * @author chitanda
   * @date 2021-06-23 10:06:26
   * @type {string[]}
   */
  readonly messages: TerminalMessageItem[] = [];

  /**
   * 界面隐藏时新的未读消息
   *
   * @author chitanda
   * @date 2021-06-23 17:06:17
   * @type {TerminalMessageItem[]}
   */
  readonly newMessages: TerminalMessageItem[] = [];

  /**
   * 是否在激活的terminal中
   *
   * @author chitanda
   * @date 2021-06-23 18:06:36
   * @protected
   */
  protected activeTerminal = false;

  /**
   * 写入消息
   *
   * @author chitanda
   * @date 2021-06-23 11:06:29
   * @param {TerminalMessageItem} msgItem
   */
  write(msg: string, type: string): void {
    this.sendMessage(new TerminalMessageItem(msg, type));
  }

  /**
   * 写入消息并换行
   *
   * @author chitanda
   * @date 2021-06-23 11:06:00
   * @param {string} msg
   * @param {string} [type]
   */
  writeln(msg: string, type?: string): void {
    this.sendMessage(new TerminalMessageItem(msg, type, 'ln'));
  }

  /**
   * 输出进度条模式消息
   *
   * @author chitanda
   * @date 2021-06-23 11:06:35
   * @param {string} msg
   * @param {string} [type]
   */
  progress(msg: string, type?: string): void {
    this.sendMessage(new TerminalMessageItem(msg, type, 'progress'));
  }

  /**
   * 发送消息
   *
   * @author chitanda
   * @date 2021-06-23 18:06:30
   * @protected
   * @param {TerminalMessageItem} item
   */
  protected sendMessage(item: TerminalMessageItem): void {
    if (!this.activeTerminal) {
      this.newMessages.push(item);
    } else {
      this.messages.push(item);
      this.evt.emit('message', item);
    }
  }

  /**
   * 清空缓存
   *
   * @author chitanda
   * @date 2021-06-23 10:06:02
   */
  clear(): void {
    clearAll(this.messages);
  }

  /**
   * 读取所有信息
   *
   * @author chitanda
   * @date 2021-06-23 18:06:46
   * @return {*}  {TerminalMessageItem[]}
   */
  readAll(): TerminalMessageItem[] {
    if (this.newMessages.length > 0) {
      if (this.messages.length > 0) {
        this.messages.push(new TerminalMessageItem(this.splitLineText, null, 'ln'));
      }
      this.messages.push(...this.newMessages);
      clearAll(this.newMessages);
      if (this.group) {
        this.group.emitChange();
      }
    }
    return this.messages;
  }

  /**
   * 启用terminal
   *
   * @author chitanda
   * @date 2021-06-23 19:06:35
   */
  enable(): void {
    this.activeTerminal = true;
  }

  /**
   * 未启用terminal
   *
   * @author chitanda
   * @date 2021-06-23 19:06:36
   */
  disable(): void {
    this.activeTerminal = false;
  }
}
